package com.liao.ticket.controller;

import com.alibaba.fastjson.JSON;
import com.liao.ticket.common.exception.ErrorCode;
import com.liao.ticket.service.AirlineService;
import com.liao.ticket.util.AdminThreadLocal;
import com.liao.ticket.vo.Result;
import com.liao.ticket.vo.params.AirlineParams;
import com.liao.ticket.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 接口测试成功
 * 10.16 11:50  --
 * 1.0.0
 */
@RestController
@RequestMapping("airline")
public class AirlineController {
    @Autowired
    private AirlineService airlineService;

    // 通过 航空公司id 查询 航空公司
    @GetMapping("one/{id}")
    public Result queryOneById(@PathVariable Long id){
        return airlineService.selectOneById(id);
    }

    // 通过 航空公司名 查询 航空公司
    @GetMapping("one")
    public Result queryListByName(@RequestBody Map map) {
        Object pageParamsJson = map.get("pageParams");
        String name = map.get("name").toString();
        PageParams pageParams = JSON.parseObject(pageParamsJson.toString(), PageParams.class);
        return airlineService.selectListByName(name,pageParams);
    }

    // 查询 所有航空公司
    @GetMapping("all")
    public Result queryAll(@RequestBody PageParams pageParams){
        return airlineService.selectAll(pageParams);
    }

    // 添加 航空公司
    @PostMapping("add")
    public Result addOne(@RequestBody AirlineParams airlineParams){
        if (AdminThreadLocal.get() == null) {
            return Result.fail(ErrorCode.USER_NO_POWER);
        }
        return airlineService.addOne(airlineParams);
    }

    // 通过 航空公司id 修改 航空公司
    @PostMapping("update")
    public Result updateOneById(@RequestBody AirlineParams airlineParams){
        if (AdminThreadLocal.get() == null) {
            return Result.fail(ErrorCode.USER_NO_POWER);
        }
        return airlineService.updateOne(airlineParams);
    }

    // 通过 航空公司id 删除 航空公司
    @GetMapping("delete/{id}")
    public Result deleteOneById(@PathVariable Long id){
        if (AdminThreadLocal.get() == null) {
            return Result.fail(ErrorCode.USER_NO_POWER);
        }
        return airlineService.deleteOneById(id);
    }

}
